export class SectTaskManager {
  /** 挂机宗门任务收益时间(秒) */
  private hangUpTaskIncomeTime = 60 * 2;
  private hangUpUseTime = 0;
  private hangUpTaskProgress = 0;
  /** 是否开始 */
  private isStart = false;
  /** 当前进行中的挂机任务 id */
  private currentHangUpTaskId: string | null = null;

  /** 更新进度条 */
  public updateProgress() {
    const progress = Math.round(
      (this.hangUpUseTime / this.getHangUpTaskIncomeTime()) * 100
    );
    this.hangUpTaskProgress = Math.min(100, progress);
  }

  /** 获取剩余多少秒 */
  public getLeftTime() {
    return Math.max(
      0,
      Math.round(this.getHangUpTaskIncomeTime() - this.hangUpUseTime)
    );
  }

  /** 获取进度 */
  public getProgress() {
    return this.hangUpTaskProgress;
  }

  /** 重置进度 */
  public resetProgress() {
    this.hangUpUseTime = 0;
    this.hangUpTaskProgress = 0;
  }

  /** 宗门挂机任务更新方法 */
  public updateHangUpTask(
    dt: number,
    options?: {
      onComplete?: () => void;
    }
  ) {
    if (!this.isStart) return;
    this.hangUpUseTime += dt;
    if (this.getHangUpUseTime() >= this.getHangUpTaskIncomeTime()) {
      this.hangUpUseTime = 0;
      options?.onComplete?.();
    }
    this.updateProgress();
  }

  /** 设置当前进行中的挂机任务 id */
  public setCurrentHangUpTaskId(hangUpTaskId: string | null) {
    this.currentHangUpTaskId = hangUpTaskId;
  }

  /** 获取当前进行中的挂机任务 id */
  public getCurrentHangUpTaskId() {
    return this.currentHangUpTaskId;
  }

  /** 开始挂机任务 */
  public startHangUpTask() {
    if (this.isStart) return;
    this.isStart = true;
  }

  /** 停止挂机任务 */
  public stopHangUpTask() {
    if (!this.isStart) return;
    this.isStart = false;
  }

  /** 获取宗门挂机任务需要的时间 */
  public getHangUpTaskIncomeTime() {
    return this.hangUpTaskIncomeTime;
  }

  /** 设置已用时间 */
  public setHangUpUseTime(value: number) {
    this.hangUpUseTime = value;
  }

  /** 获取已用时间 */
  public getHangUpUseTime() {
    return this.hangUpUseTime;
  }
}
